﻿using aier.dataaccess.model;
using Dapper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace aier.dataaccess.dapper
{
    public class XrmOptionDal
    {
        public DapperHelper dapperHelper { get; set; }


        public IEnumerable<XrmOption> GetXrmOptionByEntity(string entityName, string attrName)
        {
           return  dapperHelper.ExecueWithoutTransaction<IEnumerable<XrmOption>>(a =>
            {
                string sqltext = @"select sm.[Value] as display,sm.AttributeValue as value from dbo.StringMap as sm
                                     /**where**/";
                SqlBuilder sqlbuilder = new SqlBuilder();
                var sqlTemplate = sqlbuilder.AddTemplate(sqltext);

                sqlbuilder.Where(@"sm.ObjectTypeCode = (select distinct entity.ObjectTypeCode from MetadataSchema.Entity as entity  where name =@entityName)
                                    and sm.AttributeName = @attrName ",
                                        new { entityName = entityName, attrName = attrName });


                return a.Query<XrmOption>(sqlTemplate.RawSql, sqlTemplate.Parameters);

            });
            


        }
    }
}
